//
//  MYGAddMediaViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/23.
//

import UIKit
import QMUIKit

class MYGAddMediaViewController: MYGBaseController {

    var id:String = ""
    var content: String = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "编辑媒体简介"
        
        view.backgroundColor = .white
        view.addSubview(contentLabel)
        view.addSubview(textView)
        view.addSubview(currentButton)
       
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(15)
        }
        
        textView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalTo(contentLabel.snp.bottom).offset(20)
            make.height.equalTo(200)
        }
        
        currentButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.right.equalToSuperview().inset(30)
            make.height.equalTo(44)
            make.top.equalTo(textView.snp.bottom).offset(50)
        }
        
        if let data = content.data(using: .utf8), let attr = try? NSAttributedString(
            data: data,
            options: [
                .documentType: NSAttributedString.DocumentType.html,
                .characterEncoding: String.Encoding.utf8.rawValue
            ],
            documentAttributes: nil
                
        ) {
            
            textView.attributedText = attr
        }
//        textView.text = content
    }
    

    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(15)
        label.textColor = .rgba(51, 51, 51, 1)
        label.text = "自我介绍"
        return label
    }()
    
    
    private lazy var textView: QMUITextView = {
        let tv = QMUITextView()
        tv.placeholder = "请描述您遇到的问题或您的建议"
        tv.placeholderColor = .rgba(153, 153, 153, 1)
        tv.font = .regular(15)
        tv.textColor = .rgba(51, 51, 51)
        tv.backgroundColor = .rgba(245, 246, 248, 1)
        return tv
    }()
  
    private lazy var currentButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("确认提交", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .rgba(162, 146, 254, 1)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 20
        btn.addTarget(self, action: #selector(submitButton), for: .touchUpInside)
        return btn
    }()
    
    @objc func submitButton() {
        
         content = textView.text
        if content.isEmpty {
            MMYGHUD.showToast(text: "请输入内容")
            return
        }
        
        MYGLoading.show()
        MMYGNetwork<MYGResumeApi,MYGResumeModel>.request(target: .editMedia(id: id, content: content)) { _ in
            MYGLoading.hide()
            MMYGHUD.showToast(text: "修改成功"){
                self.navigationController?.popViewController(animated: true)
            }
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }

}
